<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      style="padding-right: 30px"
    >
      <el-form-item label="品牌" prop="car_id">
        <el-select v-model="form.car_id" clearable placeholder="请选择品牌">
          <el-option
            v-for="item in carList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车型" prop="type_id">
        <el-select v-model="form.type_id" clearable placeholder="请选择车型">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="功能名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="英文名" prop="name_en">
        <el-input v-model="form.name_en" />
      </el-form-item>
      <el-form-item label="功能编号" prop="number_no">
        <el-input v-model="form.number_no" />
      </el-form-item>
      <el-form-item label="功能值ID" prop="play_id">
        <el-input v-model="form.play_id" />
      </el-form-item>
      <el-form-item label="数值" prop="play_val">
        <el-input v-model="form.play_val" />
      </el-form-item>
      <el-form-item label="对接编号" prop="play_no">
        <el-input v-model="form.play_no" />
      </el-form-item>
      <el-form-item label="功能年份" prop="play_year">
        <el-date-picker
          v-model="form.play_year"
          placeholder="选择年份"
          type="year"
          value-format="yyyy"
        />
      </el-form-item>
      <el-form-item label="起始位" prop="start_val">
        <el-input v-model="form.start_val" />
      </el-form-item>
      <el-form-item label="位长度" prop="val_length">
        <el-input v-model="form.val_length" />
      </el-form-item>
      <el-form-item label="OBD" prop="obd">
        <el-radio v-model="form.obd" :label="1">是</el-radio>
        <el-radio v-model="form.obd" :label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="允许下载" prop="allow">
        <el-radio v-model="form.allow" :label="1">是</el-radio>
        <el-radio v-model="form.allow" :label="2">否</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doPlayEdit as doEdit } from '@/api/car'

  export default {
    name: 'CarPlayEdit',
    data() {
      return {
        form: {
          id: '',
          car_id: '',
          type_id: '',
          name: '',
          name_en: '',
          number_no: '',
          play_no: '',
          play_val: '',
          play_year: '',
          start_val: '',
          val_length: '',
          obd: 2,
          allow: 2,
        },
        carList: [],
        originalTypeList: [], //全部
        typeList: [], //联动
        rules: {
          car_id: [
            { required: true, trigger: 'change', message: '请选择品牌' },
          ],
          type_id: [
            { required: true, trigger: 'change', message: '请选择车型' },
          ],
          name: [
            { required: true, trigger: 'blur', message: '请输入功能名称' },
          ],
          number_no: [
            { required: true, trigger: 'blur', message: '请输入功能编码' },
          ],
          play_id: [
            { required: true, trigger: 'blur', message: '请输入功能值ID' },
          ],
          play_val: [
            { required: true, trigger: 'blur', message: '请输入数值' },
          ],
          play_no: [
            { required: true, trigger: 'blur', message: '请输入对接编号' },
          ],
          play_year: [
            { required: true, trigger: 'change', message: '请选择年份' },
          ],
          start_val: [
            { required: true, trigger: 'blur', message: '请输入起始位' },
          ],
          val_length: [
            { required: true, trigger: 'blur', message: '请输入位长度' },
          ],
          obd: [{ required: true, trigger: 'change', message: '请选择obd' }],
          allow: [
            {
              required: true,
              trigger: 'change',
              message: '请选择是否允许下载',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    watch: {
      'form.car_id': {
        handler(newV) {
          this.typeList = this.originalTypeList
          if (newV) {
            this.typeList = this.originalTypeList.filter((item) => {
              return item.car_id == newV
            })
          }
          console.log(this.typeList)
        },
        immediate: true,
      },
    },
    // computed: {
    //   typeNewList() {
    //     return this.originalTypeList.filter((item) => {
    //       return item.car_id == this.form.car_id
    //     })
    //   },
    // },
    created() {},
    methods: {
      showEdit(carList, typeList, row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        console.log(this.form)
        this.originalTypeList = typeList
        this.carList = carList
        this.$nextTick(() => {
          this.dialogFormVisible = true
        })
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(this.form)
            this.$baseMessage(message, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      updateImg(val) {
        console.log(val)
        this.form.pic = val[0]
      },
    },
  }
</script>
